package org.guegan.util.dircompare.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Factory implements ApplicationContextAware {
	private static Factory instance = null;
	
	private Factory() {
		context = new ClassPathXmlApplicationContext(new String[] {"spring-beans.xml"});
	}
	
	public static Factory getFactory() {
		if (instance == null) {
			instance = new Factory();
		}
		return instance;
	}

	private ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.context = applicationContext;
	}
	
	public ApplicationContext getContext() {
		return context;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		T ret = null;
		ret = (T) getFactory().getContext().getBean(name);
		return ret;
	}
}
